package it.espsoft.easysuperenalotto.dao;

import java.util.ArrayList;
import java.util.List;

import it.espsoft.easysuperenalotto.bean.EstrazioneBean;
import it.espsoft.easysuperenalotto.db.DB;
import it.espsoft.easysuperenalotto.db.DataBaseCreationHelper;
import it.espsoft.easysuperenalotto.db.TableNames;
import it.espsoft.easysuperenalotto.utils.Utils;
import android.content.ContentValues;
import android.database.Cursor;

public class EstrazioneDAO implements TableNames{

	public static void insert(EstrazioneBean eb) {
		ContentValues cv = new ContentValues();
		cv.put("jackpot", eb.getJackpot());
		cv.put("primo", eb.getPrimo());
		cv.put("secondo", eb.getSecondo());
		cv.put("terzo", eb.getTerzo());
		cv.put("quarto", eb.getQuarto());
		cv.put("quinto", eb.getQuinto());
		cv.put("sesto", eb.getSesto());
		cv.put("superstar", eb.getSuperstar());
		cv.put("dataConcorso", Utils.formatDate(eb.getDataConcorso()));
		cv.put("numConcorso", eb.getNumConcorso());
		cv.put("jolly", eb.getJolly());
		DB.getDataBase().insert(ESTRAZIONI_TABLE_NAME, null, cv);
	}
	
	public static List<EstrazioneBean> getEstrazioni(){
		Cursor cursor = DB.getDataBase().query(DataBaseCreationHelper.ESTRAZIONI_TABLE_NAME, null,null,null,null,null,"id desc");
		return estrazioniFromCursor(cursor);
	}

	public static EstrazioneBean getUltimaEstrazione(){
		List<EstrazioneBean> result = getEstrazioni();
		if(result.size()>0){
			return getEstrazioni().get(0);
		}else{
			return null;
		}
	}
	
	private static List<EstrazioneBean> estrazioniFromCursor(Cursor cursor) {
		List<EstrazioneBean> result = new ArrayList<EstrazioneBean>();
		
		int idClmn = cursor.getColumnIndex("id");
		int numConcorsoClmn = cursor.getColumnIndex("numConcorso"); 
		int dataConcorsoClmn = cursor.getColumnIndex("dataConcorso");
        int jackpotClmn = cursor.getColumnIndex("jackpot");
        int jollyClmn = cursor.getColumnIndex("jolly");
        int superstarClmn = cursor.getColumnIndex("superstar");
        int primoClmn = cursor.getColumnIndex("primo");
        int secondoClmn = cursor.getColumnIndex("secondo");
        int terzoClmn = cursor.getColumnIndex("terzo");
        int quartoClmn = cursor.getColumnIndex("quarto");
        int quintoClmn = cursor.getColumnIndex("quinto");
        int sestoClmn = cursor.getColumnIndex("sesto");
        
        if(cursor.moveToFirst()){
        	do{
        		EstrazioneBean eb = new EstrazioneBean();
        		eb.setId(cursor.getInt(idClmn));
        		eb.setNumConcorso(cursor.getInt(numConcorsoClmn));
        		eb.setJackpot(cursor.getString(jackpotClmn));
        		eb.setDataConcorso(Utils.parseDate(cursor.getString(dataConcorsoClmn)));
        		eb.setJolly(cursor.getString(jollyClmn));
        		eb.setSuperstar(cursor.getString(superstarClmn));
        		eb.setPrimo(cursor.getString(primoClmn));
        		eb.setSecondo(cursor.getString(secondoClmn));
        		eb.setTerzo(cursor.getString(terzoClmn));
        		eb.setQuarto(cursor.getString(quartoClmn));
        		eb.setQuinto(cursor.getString(quintoClmn));
        		eb.setSesto(cursor.getString(sestoClmn));
        		result.add(eb);
        	}while(cursor.moveToNext());
        }
		return result;
	}
	
	public static int deleteAllEstrazioni(){
		return DB.getDataBase().delete(ESTRAZIONI_TABLE_NAME, null, null);
	}

	public static int updateEstrazioneByConcorso(EstrazioneBean eb) {
		ContentValues cv = new ContentValues();
		cv.put("jackpot", eb.getJackpot());
		return DB.getDataBase().update(ESTRAZIONI_TABLE_NAME, cv, "numConcorso=?", new String[] {""+eb.getNumConcorso()});		
	}
}
